<%@page import="com.loyal.loyalerp.basedata.company.model.GyCompany"%>
<%@page import="com.loyal.loyalerp.humenresource.role.model.GyRole"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色信息编辑</title>
<style>
	#manage-form{
		display: table;
	}
	.table-row{
		display: table-row;
	}
	.table-row>*{
		display: table-cell;
		margin-left: 20px;
		margin-top: 5px;
	}

</style>
</head>
<body style="align: center;">
	<div style="margin-top: 10px; margin-left: 30px;">
		<form id="manage-form" action="">
			<div class="table-row">
				<label for="jobcode">公司名字:</label> <select style="width: 100px; height: 28px;"
					name="companyname" id="companyname">
					<option id="0" value="0">无</option>
					<%
						List<GyCompany> companys = (List<GyCompany>) request.getAttribute("companys");
						Iterator it = companys.iterator();
						while (it.hasNext()) {
							GyCompany company = (GyCompany) it.next();
					%>
					<option id="<%=company.getCompanyname()%>" value="<%=company.getCompanyname()%>"><%=company.getCompanyname()%></option>
					<%
						}
					%>

				</select>
			</div>
			<div class="table-row">
				<label for="jobcode">角色編碼:</label> 
				<input type="text" class="ui-input" name="tradename" id="roleCode">
			</div>
			<div class="table-row">
				<label for="jobcode">角色名稱:</label> 
				<input type="text" class="ui-input" name="tradename" id="roleName">
			</div>
			<div class="table-row">
				<label for="jobcode">備注:</label> 
				<input type="text" class="ui-input" name="tradename" id="remark">
			</div>
			
			<div class="table-row">
				<label for="jobcode">状态:</label> 
				<select
					style="width: 100px; height: 28px;" name="active" id="active">
						<option id="1" value="1">可用</option>
						<option id="0" value="0">不可用</option>
				</select>			
			</div>
		</form>
	</div>

	<script type="text/javascript">
		var api = frameElement.api;
		var operate = api.data.operate;
		var rowid = api.data.rowid;
		var rowdata = api.data.rowdata;
		var CallBack = api.data.callback;
		$(function() {
			initPopBtns();
			if (operate == "edit") {
				var all_options = document.getElementById("companyname").options;
				for (i = 0; i < all_options.length; i++) {
					if (all_options[i].id == rowdata.companyid) // 根据option标签的ID来进行判断  测试的代码这里是两个等号
					{
						all_options[i].selected = true;
					}
				}
				$("#roleCode").val(rowdata.rolecode);
				$("#roleName").val(rowdata.rolename);
				$("#remark").val(rowdata.remark);
				all_options = document.getElementById('active').options;
				for (i = 0; i < all_options.length; i++) {
					if (all_options[i].id == String(1)&&rowdata.active==String('可用')) // 根据option标签的ID来进行判断  测试的代码这里是两个等号
					{
						all_options[i].selected = true;
					}else if (all_options[i].id == String(0)&&rowdata.active==String('不可用')) // 根据option标签的ID来进行判断  测试的代码这里是两个等号
					{
						all_options[i].selected = true;
					}
				}
			}
		})
		function initPopBtns() {
			var btnname = "add" == operate ? [ "保存", "关闭" ] : [ "确定", "取消" ];
			api.button({
				id : "confirm",
				name : btnname[0],
				focus : true,
				callback : function(e) {
					var companyname=$("#companyname").val();
					var rolecode = $("#roleCode").val();
					var rolename = $("#roleName").val();
					var remark = $("#remark").val();
					var active = $("#active").val();
					if (operate == "add") {
						$.ajax({
							url : '/Role/editRole',
							type : 'POST',
							dataType : "json",
							data : {
								"id" : null,
								"companyname":companyname,
								"rolecode" : rolecode,
								"rolename" : rolename,
								"remark" : remark,
								"active" : active,
								"oper" : "add"
							},
							async : false,
							success : function(data) {
								if (data.status == 200) {
									parent.Public.tips({
										content : "添加角色成功"
									})
									CallBack();
								} else if(data.status == 888){
									parent.Public.tips({
										type : 1,
										content : "修改角色失败:没有权限" 
									})
								}else {
									parent.Public.tips({
										type : 1,
										content : "添加角色失败:" + data.msg
									})
								}
							}
						});
					} else {
						$.ajax({
							url : encodeURI("/Role/editRole"),
							type : "POST",
							data : {
								"id" : rowid,
								"companyname" : companyname,
								"rolecode" : rolecode,
								"rolename" : rolename,
								"remark" : remark,
								"active" : active,
								"oper" : "edit"
							},
							async : false,//同步,坑爹啊。。
							success : function(data) {
								if (data.status == 200) {
									parent.Public.tips({
										content : "修改角色成功"
									})
									CallBack();
								} else if(data.status == 888){
									parent.Public.tips({
										type : 1,
										content : "修改角色失败:没有权限" 
									})
								}else {
									parent.Public.tips({
										type : 1,
										content : "修改角色失败:" + data.msg
									})
								}
							}
						});
					}
				}
			}, {
				id : "cancel",
				name : btnname[1]
			})
		}
	</script>
</body>
</html>